package org.jeecg.modules.carRegister.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class TemplateMessageUtil {

    private final RedisUtil redisUtil;

    //太维科技公众号
//	 private static final String appid = "wx35cfb8619f70829d";
//	 private static final String secret = "ad9fa5ffe510db2f88d3164c119444c1";
    //太维科技服务号
    private static final String APPID = "wx49ad1b4481e6b941";
    private static final String SECRET = "1dc20043560e708a2b6991a40c32a160";

    private static final String WX_TOKEN = "hy_wx_token";

    @Autowired
    public TemplateMessageUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    /**
     * 来访货车待审核提醒
     * @param touser 接收者openid
     * @param plateNo 车牌号
     * @param driverName 驾驶员
     */
    public void sendCarOA(String touser, String plateNo, String driverName) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        String template_id = "mx010kawSuPOTAPY1Xz--P1HII2Ng9NObvloHuAIvbk";
        String url = "http://register.ydyjk.com.cn/?location=%E6%B5%B7%E6%B4%8B%E7%9F%B3%E6%B2%B9%E5%B7%A5%E7%A8%8B%E5%BB%BA%E9%80%A0%E5%85%AC%E5%8F%B8%E4%B8%B4%E6%B8%AF%E5%9C%BA%E5%9C%B09";
        JSONObject jsonObj=new JSONObject();
        JSONObject data=new JSONObject();

        JSONObject car_number3=new JSONObject();
        car_number3.put("value",plateNo);

        JSONObject thing2=new JSONObject();
        thing2.put("value", driverName);

        data.put("car_number3",car_number3);
        data.put("thing2",thing2);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id",template_id);//模板ID
        jsonObj.put("url", url);//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 车辆预约已驳回通知
     * @param touser 接收者openid
     * @param plateNo 车牌号
     * @param driverName 驾驶员
     * @param type 类型：9内部，8司机
     */
    public void sendRejectOA(String touser, String plateNo, String driverName, String type) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        String template_id = "-rXRTkNzeq55Prlzbm6fsYQe5_lzZfkTe5MO6luPv4A";
        String url = "http://register.ydyjk.com.cn/?location=%E6%B5%B7%E6%B4%8B%E7%9F%B3%E6%B2%B9%E5%B7%A5%E7%A8" +
                "%8B%E5%BB%BA%E9%80%A0%E5%85%AC%E5%8F%B8%E4%B8%B4%E6%B8%AF%E5%9C%BA%E5%9C%B0" + type;
        JSONObject jsonObj=new JSONObject();
        JSONObject data=new JSONObject();

        JSONObject car_number2=new JSONObject();
        car_number2.put("value",plateNo);

        JSONObject thing1=new JSONObject();
        thing1.put("value", driverName);

        JSONObject time4=new JSONObject();
        time4.put("value", DateUtils.formatDate("yyyy年MM月dd日 HH:mm:ss"));

        data.put("car_number2",car_number2);
        data.put("thing1",thing1);
        data.put("time4",time4);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id",template_id);//模板ID
        jsonObj.put("url", url);//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 货车预约审核通过通知
     * @param touser 接收者openid
     * @param plateNo 车牌号
     * @param driverName 驾驶员
     */
    public void sendSuccessOA(String touser, String plateNo, String driverName) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        String template_id = "IbxfUfkhl6nVGPXlb1HObB8rArEo5ZQL2w_Y4gva-gU";
        String url = "http://register.ydyjk.com.cn/?location=%E6%B5%B7%E6%B4%8B%E7%9F%B3%E6%B2%B9%E5%B7%A5%E7%A8%8B%E5%BB%BA%E9%80%A0%E5%85%AC%E5%8F%B8%E4%B8%B4%E6%B8%AF%E5%9C%BA%E5%9C%B08";
        JSONObject jsonObj=new JSONObject();
        JSONObject data=new JSONObject();

        JSONObject car_number2=new JSONObject();
        car_number2.put("value",plateNo);

        JSONObject thing1=new JSONObject();
        thing1.put("value", driverName);

        data.put("car_number2",car_number2);
        data.put("thing1",thing1);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id",template_id);//模板ID
        jsonObj.put("url", url);//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    public String getToken() {
        if(redisUtil.hasKey(WX_TOKEN)){
            Object token=redisUtil.get(WX_TOKEN);
            if(token!=null&& !StringUtils.isEmpty(token.toString())){
                return token.toString();
            }else{
                redisUtil.set(WX_TOKEN,getWXToken(),7000);
            }
        }else{
            redisUtil.set(WX_TOKEN,getWXToken(),7000);
        }
        return redisUtil.get(WX_TOKEN).toString();
    }

    public String getWXToken() {
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID + "&secret=" + SECRET;
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObj = JSONObject.parseObject(forObject);
        if(jsonObj.containsKey("access_token")){
            return jsonObj.getString("access_token");
        } else {
            log.info("获取token失败: {}", jsonObj);
        }
        return "";
    }
}
